/**********************************************************************
 *** *                                                            * ***
 *** *  Copyright (c) 2013 Harmonia Holdings Group LLC            * ***
 *** *                                                            * ***
 *** * Permission is hereby granted to use, modify, copy, and     * ***
 *** * distribute this software and its documentation for any     * ***
 *** * purpose only without profit, provided that the above       * ***
 *** * Copyright Notice appears in all copies and that both the   * ***
 *** * Copyright Notice and this Permission Notice appears in     * ***
 *** * every copy of supporting documentation.  No title to nor   * ***
 *** * ownership of the software is transferred hereby.  The name * ***
 *** * of Harmonia Holdings Group LLC shall not be used in        * ***
 *** * advertising or publicity related to the distribution of    * ***
 *** * the software without specific, written, prior permission.  * ***
 *** * This software is provided as-delivered without expressed   * ***
 *** * or implied warranty.  Harmonia Holdings Group LLC          * ***
 *** * makes no representation about the suitability of this      * ***
 *** * software for any purpose and accepts no responsibility for * ***
 *** * its use.                                                   * ***
 *** *                                                            * ***
 *** ************************************************************** ***
 *** *                                                            * ***
 *** * This program is free software; you can redistribute it     * ***
 *** * and/or modify it under the terms of the GNU General Public * ***
 *** * License as published by the Free Software Foundation;      * ***
 *** * either version 2 of the License, or (at your option) any   * ***
 *** * later version.                                             * ***
 *** *                                                            * ***
 *** * This program is distributed in the hope that it will be    * ***
 *** * useful, but WITHOUT ANY WARRANTY; without even the implied * ***
 *** * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR    * ***
 *** * PURPOSE.  See the GNU General Public License for more      * ***
 *** * details.                                                   * ***
 *** *                                                            * ***
 *** * You should have received a copy of the GNU General Public  * ***
 *** * License along with this program; if not, write to the Free * ***
 *** * Software Foundation, Inc., 51 Franklin Street, Fifth       * ***
 *** * Floor, Boston, MA 02110-1301, USA.                         * ***
 *** *                                                            * ***
 *** * For more information: http://www.gnu.org/licenses/gpl.html * ***
 *** *                                                            * ***
 **********************************************************************/
package org.etexascode.interrep.datamodel;
import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import org.apache.commons.lang3.builder.HashCodeBuilder;

/**
 * The detector manager model.
 * @author ablatt
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
@XmlSeeAlso(Detector.class)
public class DetectorManager implements Serializable {
    /** Serial ID. */
    @XmlTransient
    private static final long serialVersionUID = 7762341650692544779L;

    /** The map of detectors, keyed by detector id. */
    @XmlElement
    @XmlJavaTypeAdapter(ManagerAdapter.class)
    private Map<Integer, Detector> detectors;

    /** Constructor. */
    public DetectorManager() {
	detectors = new HashMap<Integer, Detector>();
    }

    /**
     * Adds a detector to the list.
     * @param key The key.
     * @param value The detector.
     */
    public void addDetector(Integer key, Detector value) {
	detectors.put(key, value);
    }

    /** Manager as a string representation. */
    public void clearDetectors() {
	detectors = new HashMap<Integer, Detector>();
    }

    /** Checks if 2 detector managers are equal. */
    @Override
    public boolean equals(Object obj) {
	if(obj instanceof DetectorManager){
	    DetectorManager dm = (DetectorManager) obj;

	    Set<Integer> thisKeys = detectors.keySet();
	    Set<Integer> dmKeys = dm.getKeys();

	    if(thisKeys.size() != dmKeys.size() || !(thisKeys.containsAll(dmKeys) && dmKeys.containsAll(thisKeys))) {
		return false;
	    }

	    for(Integer i:dmKeys) {
		if(!detectors.get(i).equals(dm.detectors.get(i))) {
		    return false;
		}
	    }

	    return true;
	} else {
	    return false;
	}

    }
	
	/**
	 * HashCode operation. 
	 * @return A hashcode of the data. 
	 */
	@Override
	public int hashCode() {
		return new HashCodeBuilder(23, 11).
				toHashCode();
	}
	
    /**
     * Gets the detector with the given ID..
     * @param id The ID.
     * @return The detector.
     */
    public Detector getDetector(Integer id) {
	return detectors.get(id);
    }

    /**
     * Gets all the detectors.
     * @return The collection of detectors.
     */
    public Collection<Detector> getDetectorCollection() {
	return detectors.values();
    }

    /**
     * Gets the keys.
     * @return The list of keys.
     */
    public Set<Integer> getKeys() {
	return detectors.keySet();
    }

    /**
     * Create a string out of this detector manager.
     */
    @Override
    public String toString() {
	StringBuilder ret = new StringBuilder();
	Set<Integer> keyset = detectors.keySet();

	ret.append("Detectors:\n");

	for(Integer i:keyset) {
	    ret.append(i.toString());
	    ret.append(":");
	    ret.append(detectors.get(i).toString());
	}

	ret.append("=====\n\n");

	return ret.toString();
    }
}